import { NextApiRequest, NextApiResponse } from 'next'
import {withIronSessionApiRoute} from 'iron-session/next'
import {prepareConnection} from 'db/index'
import { User } from 'db/entity/index'
import {ironOptions} from 'config/index'
import { ISession} from 'pages/api/index'
import { EXCEPTION_USER } from 'pages/api/config/codes'

export default withIronSessionApiRoute(detail,ironOptions)

async function detail(req: NextApiRequest, res: NextApiResponse) {
    const sesssion: ISession = req.session
    const {userId} = sesssion

    // 连接数据库
    const db = await prepareConnection()
    const UserRepo = db.getRepository(User)
    const user = await UserRepo.findOne({where: {id: Number(userId)}})
    if (user) {
        res.status(200).json({code: 0, msg: '', data: {userInfo:user}})
    } else {
        res?.status(200).json({...EXCEPTION_USER.NOT_FOUND})
    }
}